/**
 * =================================================
 *
 * @copyright 杭州龙骞科技有限公司 2012-1014
 * =================================================
 */
package com.boilerCloud.util;

import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 字符串处理相关工具类
 *
 * @author qfq
 * @date 2012-10-16 15:37:14
 * @version 1.0
 */
public class StringUtil {
	private static Logger log = LoggerFactory.getLogger(StringUtil.class);

	/* Date类型转化到String类型 */
	public static String DateToString(java.util.Date date, int type) {
		String result = null;
		String format = "yyyy-MM-dd";
		if (type == 0) {
			format = "yyyy-MM-dd";
		}
		if (type == 1) {
			format = "yyyy/MM/dd";
		} else if (type == 2) {
			format = "yyyyMMdd";
		} else if (type == 21) {
			format = "yyyyMM";
		} else if (type == 3) {
			format = "MM/dd/yy";
		} else if (type == 4) {
			format = "yyyy-MM-dd HH:mm:ss";
		} else if (type == 5) {
			format = "yyyyMMddHHmmss";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		if (date != null) {
			result = formatter.format(date);
		} else {
			result = "";
		}
		return result;
	}

	/* String类型到Date类型转化 */

	public static java.util.Date StringToDate(String strDate, int type) {
		java.util.Date result = null;
		String format = "yyyy-MM-dd";
		if (type == 0) {
			format = "yyyy-MM-dd";
		}
		if (type == 1) {
			format = "yyyy/MM/dd";
		} else if (type == 2) {
			format = "yyyyMMdd";
		} else if (type == 21) {
			format = "yyyyMM";
		} else if (type == 3) {
			format = "MM/dd/yy";
		} else if (type == 4) {
			format = "yyyy-MM-dd HH:mm:ss";
		} else if (type == 5) {
			format = "yyyyMMddHHmm";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		if (strDate != null && !strDate.equals("")) {
			try {
				result = formatter.parse(strDate);
			} catch (ParseException ex) {
				result = null;
			}
		}
		return result;
	}

	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
	}

	
	/**
	 * 判断字符串是否为空或null，为空返回true
	 *
	 * @param str
	 *            待判定字符串
	 * @return 是否为空或null
	 */
	public static boolean isEmpty(String str) {
		return (str == null || "".equals(str.trim()));
	}

	/**
	 * 判断参数是否为空，只有其中有一个参数为空则返回true，否则返回false
	 * 
	 * @author:zzy
	 * @date 2013-11-26上午10:42:34
	 * @modifier:
	 * @modify_description:
	 * @param objects
	 * @return
	 * @version v1.0.0
	 */
	public static boolean isEmpty(Object... objects) {
		boolean result = false;
		for (Object object : objects) {
			if (object instanceof String) {
				if (object == null || "".equals(object)) {
					return true;
				}
			}
			if (object instanceof Integer) {
				if (object == null || 0 == (Integer) object) {
					return true;
				}
			}
			if (object instanceof Double) {
				if (object == null || 0 == (Double) object) {
					return true;
				}
			}
			if (object instanceof Float) {
				if (object == null || 0 == (Float) object) {
					return true;
				}
			}
			if (object instanceof Long) {
				if (object == null || 0 == (Long) object) {
					return true;
				}
			}
			if (object instanceof Date) {
				if (object == null || null == (Date) object) {
					return true;
				}
			}
			if (object instanceof Timestamp) {
				if (object == null || null == (Timestamp) object) {
					return true;
				}
			}
			if (object instanceof Object) {
				if (object == null) {
					return true;
				}
			}
			if (object == null) {
				return true;
			}
		}
		return result;
	}

	/**
	 * 对象转为string格式，为空时，转为默认带入字符串
	 *
	 * @param obj
	 *            待转对象
	 * @param defaultStr
	 *            默认字符串
	 * @return 字符串
	 */
	public static String objToString(Object obj, String defaultStr) {
		if (obj == null) {
			return defaultStr;
		} else {
			return obj.toString().trim();
		}
	}

	/**
	 * 判断字符串长度是否合法
	 *
	 * @param instr
	 *            输入字符串
	 * @param minLength
	 *            最小长度
	 * @param maxLength
	 *            最大长度
	 * @return true=合法
	 */
	public static boolean chackStringLength(String instr, int minLength, int maxLength) {
		return !(instr.length() < minLength || instr.length() > maxLength);
	}

	/**
	 * 判断字符串是否全是数字
	 *
	 * @param str
	 *            输入字符串
	 * @return true=全是数字
	 */
	public static boolean isNum(String str) {
		if (null == str) {
			return false;
		}
		return str.matches("\\d+");
	}

	/**
	 * 字符串长度处理，过长切除，不足补空格.
	 *
	 * @param str
	 *            传入字符串
	 * @param length
	 *            传入指定长度(字节数)
	 * @param defaultStr
	 *            默认字符
	 * @param isRight
	 *            是否右补齐
	 * @return 返回指定长度的字符串
	 */
	public static String supplyString(String str, int length, String defaultStr, boolean isRight) {
		if (str != null) {
			int len = str.length();
			if (len < length) {
				if (isRight) {
					for (int i = 0; i < (length - len); i++) {
						str = str + defaultStr;
					}
				} else {
					for (int i = 0; i < (length - len); i++) {
						str = defaultStr + str;
					}
				}
			} else {
				str = str.substring(0, length);
			}
		} else {// 空字符串
			if (isRight) {
				for (int i = 0; i < length; i++) {
					str = str + defaultStr;
				}
			} else {
				for (int i = 0; i < length; i++) {
					str = defaultStr + str;
				}
			}
		}
		return str;
	}

	/**
	 * 字符串倒转
	 *
	 * @param str
	 *            输入字符串
	 * @return 倒转后字符串
	 */
	public static String reverseStr(String str) {
		StringBuilder strbuff = new StringBuilder();
		for (int i = str.length() - 1; i >= 0; i--) {
			strbuff.append(str.charAt(i));
		}
		return strbuff.toString();
	}

	/**
	 * 生成随机几位数字
	 *
	 * @param k
	 *            生成位数
	 * @return
	 */
	public static String createRand(int k) {
		Random random = new Random();
		String sRand = "";
		for (int i = 0; i < k; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
		}
		return sRand;
	}

	public static String getHost(String host, String methodName) {
		StringBuilder resultUrl = new StringBuilder("");
		resultUrl.append(host);
		resultUrl.append("/" + methodName);
		resultUrl.append("/gzip");// 启用数据压缩
		return resultUrl.toString();
	}

	
	
	public static String stringToMD5String(String string) {
		String md5 = MD5.getMD5(string);
		md5 = md5.toUpperCase();
		return md5;
	}

	public static Integer getInteger(String str) {
		Integer intTemp = 0;
		try {
			intTemp = Integer.valueOf(str);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return intTemp;
	}

	/**
	 * 获取本机IP
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<String> getHostIp() throws Exception {
		List<String> li = new ArrayList<String>();
		Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		InetAddress ip = null;
		while (allNetInterfaces.hasMoreElements()) {
			NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
			Enumeration addresses = netInterface.getInetAddresses();
			while (addresses.hasMoreElements()) {
				ip = (InetAddress) addresses.nextElement();
				if (ip != null && ip instanceof Inet4Address) {
					System.out.println("本机的IP = " + ip.getHostAddress());
					li.add(ip.getHostAddress());
				}
			}
		}
		return li;
	}

	/**
	 * 把map类型参数拼装成url参数连接的字符串格式
	 * 
	 * @author:zzy
	 * @date 2015-4-21上午11:06:13
	 * @modifier:
	 * @date:
	 * @modify_description:
	 * @param params
	 *            待拼接的参数map
	 * @return 拼装成url参数连接的字符串
	 * @version v1.0.0
	 */
	public static String getParamString(Map<String, String> params) {
		StringBuffer result = new StringBuffer("");
		if (params == null || params.isEmpty()) {
			return "";
		}
		Set<Map.Entry<String, String>> set = params.entrySet();
		Iterator<Map.Entry<String, String>> it = set.iterator();
		if (it.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
			String param = entry.getKey() + "=" + entry.getValue();
			result.append(param);
		}
		while (it.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
			String param = entry.getKey() + "=" + entry.getValue();
			result.append("&" + param);
		}
		return result.toString();
	}
	
	
	

/*	public static void main(String[] args) throws UnsupportedEncodingException {
		
		
		 * Map<String, String> parms = new HashMap<String, String>();
		 * parms.put("pno", "ZQ03");// 渠道号 parms.put("ver",
		 * "TYYDYSB_ZQ3.1.0.2");// 版本号 parms.put("userkey",
		 * HzlqswConstant.USERPWD);// parms.put("Password","123456");
		 * parms.put("UserName", "15158087572"); parms.put("Uid", 7728+"");
		 * parms.put("Type", 1+""); String mac = "B8:C7:5D:5E:DC:D7"; String
		 * imsi=""; String imei=""; if(!StringUtil.isEmpty(imsi)){
		 * parms.put("Imsi", imsi); } if(!StringUtil.isEmpty(imei)){
		 * parms.put("Imei", imei); } if(!StringUtil.isEmpty(mac)){
		 * parms.put("Mac", mac); }
		 * 
		 * 
		 * System.out.println(buildSign(parms));// 设置签名
		 
		// System.out.println(signatureWithParams(parms));// 设置签名

	}*/
}
